package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import cqrtplm.service.FlowControlService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;

@CustomListener(category = "润通", description = "根据表单中填写的字段自动添加到对应的节点中")
@Slf4j
public class AutoAddTaskListListener implements ExecutionListener {


    @CustomListenerField(description = "按照此格式拼接(多个用;拼接):表单字段内部名称=节点名称", defaultValue = "", required = true)
    private FixedValue node;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("----执行监听润通定制-根据表单中填写的字段自动添加到对应的节点中 开始----");
        long l = System.currentTimeMillis();
        String workFlowContainerId = delegateExecution.getProcessInstanceBusinessKey();
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = delegateExecution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        String valueStr = node.getExpressionText();
        log.info("流程监听，根据表单中填写的字段自动添加到对应的节点中,id:{}", workFlowContainerId);
        FlowControlService flowControlService = SpringUtil.getBean(FlowControlService.class);
        flowControlService.autoAddTaskListByFormData(workFlowContainerId, valueStr);
        log.info("----执行监听润通定制-根据表单中填写的字段自动添加到对应的节点中 结束，耗时:{}----", System.currentTimeMillis() - l);
    }
}


